package com.controller;


import com.alibaba.fastjson.JSON;
import com.doMain.TableVO;
import com.pojo.SysDept;
import com.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("sysDept")
public class DeptController {
    @Autowired
    private SysDeptService service;

    @GetMapping("toList")
    public String toList(){
        return "deptList";
    }

    @GetMapping(value = "list",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(){
         List<SysDept> list=service.selectDeptList(null);
        List<SysDept> list1 = service.buildDeptTree(list);
        String o= JSON.toJSONString(list1);
        return o;
    }

    @GetMapping(value = "/list2",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list2(){
        List<SysDept> list=service.list();
        TableVO tableVO = new TableVO(0,50,list,"");
        String json=JSON.toJSONString(tableVO);
        return json;
    }

    @GetMapping("toAdd")
    public String toAdd(){
        return "deptAdd";
    }

    @PostMapping(value = "/add",produces = "application/json;charset=utf-8")
    public String add(SysDept dept, HttpServletResponse response) throws IOException {
        int add=service.add(dept);
        if (add>0) {
            return "redirect:toList";
        }
        return "redirect:deptAdd";

    }
    @GetMapping("toEdit")
    public String toEdit(){
        return "deptAdd";
    }

    @GetMapping("/edit")
    public void edit(SysDept dept, HttpServletResponse response) throws IOException {
        int add=service.edit(dept);
        if (add>0) {
            response.getWriter().print("1");
        }
        response.getWriter().print("0");
    }


}
